<?php

!defined('DEBUG') AND exit('Access Denied.');



user_login_check();
$user = user_read($uid);

$menu = include _include(APP_PATH.'view/admin/menu.conf.php');
$_shoplist = shop_read_uid($uid);
$shopid = array_value($_SESSION, 'adminshop', $_shoplist[0]['sid']);
$shop = shop_read($shopid);
if ($shop['uid'] != $user['uid']) {
    unset($_SESSION['adminshop']);
    http_location(url('index'));
}
$_SESSION['adminshop'] = $shopid;
$route = param(1,'setting');



if ($route == 'index') {
    $_sid = param(2);
    if(!empty($_sid)){
        $_SESSION['adminshop'] = $_sid;
        http_location(url('adminshop-setting'));
    }
    include _include(APP_PATH . 'view/admin/htm/index.htm');
} elseif ($route == 'setting') {
    if ($method == 'GET') {
        
        include _include(APP_PATH . 'view/admin/htm/setting.htm');
    } else {
        $sid = $shopid;
		$name = param('shopname','');
        $logo = param_base64('logo','');
        
        $shop = shop_read($sid);
        
        

		$_shop = array();

        ($name == '' OR $name == $shop['name'] )?:$_shop['name'] = $name;
        if($logo != ''){
            $time = time();
            $size = strlen($logo);
            $size > 40000 AND message(-1, lang('filesize_too_large', array('maxsize' => '40K', 'size' => $size)));
            $filename = $sid."_logo.png";
            $dir = $time . '/';
		    $path = $conf['upload_path'] . 'shop/' . $dir;
		    $url = $conf['upload_url'] . 'shop/' . $dir . $filename;
		    !is_dir($path) AND (mkdir($path, 0777, TRUE) OR message(-2, lang('directory_create_failed')));

		    // hook my_avatar_post_save_before.php
		    file_put_contents($path . $filename, $logo) OR message(-1, lang('write_to_file_failed'));
            $_shop['logo'] = $url;
        }
        
        shop_update($sid,$_shop);
		message(0,"修改成功");
    }
} elseif ($route == 'slide') {
    if ($method == 'GET') {
        include _include(APP_PATH . 'view/admin/htm/slide.htm');
    } else {
        $sid = $shopid;
        $slide_1 = param_base64('slide1','');
        $slide_2 = param_base64('slide2','');
        $slide_3 = param_base64('slide3','');
        $slide_4 = param_base64('slide4','');

        $shop = shop_read($sid);

		$_shop = array();

        if($slide_1 != ''){
            $time = time();
            $size = strlen($slide_1);
            //$size > 4000000 AND message(-1, lang('filesize_too_large', array('maxsize' => '40K', 'size' => $size)));
            $filename = $sid."_slide_1.png";
            $dir = $time . '/';
		    $path = $conf['upload_path'] . 'shop/' . $dir;
		    $url = $conf['upload_url'] . 'shop/' . $dir . $filename;
		    !is_dir($path) AND (mkdir($path, 0777, TRUE) OR message(-2, lang('directory_create_failed')));

		    // hook my_avatar_post_save_before.php
		    file_put_contents($path . $filename, $slide_1) OR message(-1, lang('write_to_file_failed'));
            $_shop['slide_1'] = $url;
        }
        if($slide_2 != ''){
            $time = time();
            $size = strlen($slide_2);
            //$size > 400000 AND message(-1, lang('filesize_too_large', array('maxsize' => '40K', 'size' => $size)));
            $filename = $sid."_slide_2.png";
            $dir = $time . '/';
		    $path = $conf['upload_path'] . 'shop/' . $dir;
		    $url = $conf['upload_url'] . 'shop/' . $dir . $filename;
		    !is_dir($path) AND (mkdir($path, 0777, TRUE) OR message(-2, lang('directory_create_failed')));

		    // hook my_avatar_post_save_before.php
		    file_put_contents($path . $filename, $slide_2) OR message(-1, lang('write_to_file_failed'));
            $_shop['slide_2'] = $url;
        }
        if($slide_3 != ''){
            $time = time();
            $size = strlen($slide_3);
            //$size > 400000 AND message(-1, lang('filesize_too_large', array('maxsize' => '40K', 'size' => $size)));
            $filename = $sid."_slide_3.png";
            $dir = $time . '/';
		    $path = $conf['upload_path'] . 'shop/' . $dir;
		    $url = $conf['upload_url'] . 'shop/' . $dir . $filename;
		    !is_dir($path) AND (mkdir($path, 0777, TRUE) OR message(-2, lang('directory_create_failed')));

		    // hook my_avatar_post_save_before.php
		    file_put_contents($path . $filename, $slide_3) OR message(-1, lang('write_to_file_failed'));
            $_shop['slide_3'] = $url;
        }
        if($slide_4 != ''){
            $time = time();
            $size = strlen($slide_4);
            //$size > 400000 AND message(-1, lang('filesize_too_large', array('maxsize' => '40K', 'size' => $size)));
            $filename = $sid."_slide_4.png";
            $dir = $time . '/';
		    $path = $conf['upload_path'] . 'shop/' . $dir;
		    $url = $conf['upload_url'] . 'shop/' . $dir . $filename;
		    !is_dir($path) AND (mkdir($path, 0777, TRUE) OR message(-2, lang('directory_create_failed')));

		    // hook my_avatar_post_save_before.php
		    file_put_contents($path . $filename, $slide_4) OR message(-1, lang('write_to_file_failed'));
            $_shop['slide_4'] = $url;
        }

        shop_update($sid,$_shop);
		message(0,"修改成功");
    }
} elseif ($route == 'intro') {
    if ($method == 'GET') {
        include _include(APP_PATH . 'view/admin/htm/intro.htm');
    } else {
        $sid = $shopid;
        $intro = param('intro','');

        $shop = shop_read($sid);

		$_shop = array();
        ($intro == '' OR $intro == $shop['intro'] )?:$_shop['intro'] = $intro;

        shop_update($sid,$_shop);
		message(0,"修改成功");
    }
} elseif ($route == 'seo') {
    if ($method == 'GET') {
        include _include(APP_PATH . 'view/admin/htm/seo.htm');
    } else {
        $sid = $shopid;
        $seo = param('seo','');

        $shop = shop_read($sid);

		$_shop = array();
        ($seo == '' OR $seo == $shop['seo'] )?:$_shop['seo'] = $seo;

        shop_update($sid,$_shop);
		message(0,"修改成功");
    }
} elseif ($route == 'service') {
    if ($method == 'GET') {
        include _include(APP_PATH . 'view/admin/htm/service.htm');
    } else {
        $sid = $shopid;
        $service = param('service','');

        $shop = shop_read($sid);

		$_shop = array();
        ($service == '' OR $service == $shop['service'] )?:$_shop['service'] = $service;

        shop_update($sid,$_shop);
		message(0,"修改成功");
    }
} elseif ($route == 'hot') {
    if ($method == 'GET') {
        include _include(APP_PATH . 'view/admin/htm/hot.htm');
    } else {
        $sid = $shopid;
        $hot = param('hot','');

        $shop = shop_read($sid);

		$_shop = array();
        ($hot == '' OR $hot == $shop['hot'] )?:$_shop['hot'] = $hot;

        shop_update($sid,$_shop);
		message(0,"修改成功");
    }
} elseif ($route == 'contact') {
    if ($method == 'GET') {
        $clist = xn_json_decode(array_value($shop,'contact',''));
        empty($clist) AND $clist = array('');

        $threadlist = thread_find(array('uid' => $uid),array(),1,100);

        include _include(APP_PATH . 'view/admin/htm/contact.htm');
    } else {
        $sid = $shopid;
        $tlist = param('tid',array());
        $jt = xn_json_encode($tlist);
        $_shop['contact'] = $jt;
        
        shop_update($sid,$_shop);
        message(0,"修改成功");
    }
} elseif ($route == 'case') {
    if ($method == 'GET') {
        $clist = xn_json_decode(array_value($shop,'case',''));

        empty($clist) AND $clist = array('');

        $threadlist = thread_find(array('uid' => $uid),array(),1,100);

        include _include(APP_PATH . 'view/admin/htm/case.htm');
    } else {
        $sid = $shopid;
        $tlist = param('tid',array());
        $jt = xn_json_encode($tlist);
        $_shop['case'] = $jt;
        
        shop_update($sid,$_shop);
        message(0,"修改成功");
    }
} elseif ($route == 'friendly') {
    if ($method == 'GET') {
        $flist = xn_json_decode(array_value($shop,'friendly',''));

        empty($flist) AND $flist = array(array('name'=> '','url'=>''));

        include _include(APP_PATH . 'view/admin/htm/friendly.htm');
    } else {
        $sid = $shopid;
        $namelist = param('name',array());
        $urllist = param('url',array());

        $flist = array();
        foreach ($namelist as $k => $name) {
            $flist[] = array(
                'name' => $name,
                'url' => $urllist[$k]
            );
        }
        $jf = xn_json_encode($flist);
        //print_r($jf);
        $_shop['friendly'] = $jf;
        shop_update($sid,$_shop);
        message(0,"修改成功");
    }
} elseif ($route == 'shophost') {
    if ($method == 'GET') {
        include _include(APP_PATH . 'view/admin/htm/shophost.htm');
    } else {
        $sid = $shopid;
        $shophost = param('shophost');

        empty($shophost) AND message('shophost','没有填写子域名');
        if($shophost == 'shop'){
            message('shophost','子域名不合法');
        }
        $shops = shop__read_by_host($shophost);
        $r = FALSE;
        empty($shops) AND $r = TRUE;
        if($r == TRUE){
            shop_update($sid,array('shophost' => $shophost));
            message(0,'修改成功');
        } else {
            message('shophost','子域名被占用');
        }
    }
} elseif ($route == 'lxfs') {
    if ($method == 'GET') {
        include _include(APP_PATH . 'view/admin/htm/lxfs.htm');
    } else {
        $sid = $shopid;
        $lxfs = param('lxfs','');

        $shop = shop_read($sid);

		$_shop = array();
        ($lxfs == '' OR $lxfs == $shop['lxfs'] )?:$_shop['lxfs'] = $lxfs;

        shop_update($sid,$_shop);
		message(0,"修改成功");
    }
}


?>